/* LEDC (LED Controller) basic example
   This example code is in the Public Domain (or CC0 licensed, at your option.)
   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include "driver/ledc.h"
#include "esp_err.h"
#include "PWM.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"


void ctl_PWM(uint8_t ctl_num )
{
    switch (ctl_num)
    {
        //ESP_LOGI("ctlPWM" , "ctlPWM = %d" , ctl_num);
        
    case 1://坐下
        ledc_set_duty(LEDC_MODE, LEDC_CH0, LEDC_DUTY_90);
        ledc_update_duty(LEDC_MODE, LEDC_CH0);

        ledc_set_duty(LEDC_MODE, LEDC_CH1, LEDC_DUTY_90);
        ledc_update_duty(LEDC_MODE, LEDC_CH1);

        ledc_set_duty(LEDC_MODE, LEDC_CH2, LEDC_DUTY_90);
        ledc_update_duty(LEDC_MODE, LEDC_CH2);

        ledc_set_duty(LEDC_MODE, LEDC_CH3, LEDC_DUTY_90);
        ledc_update_duty(LEDC_MODE, LEDC_CH3);

            vTaskDelay(2000 / portTICK_PERIOD_MS);

        ledc_set_duty(LEDC_MODE, LEDC_CH4, LEDC_DUTY_foot_0);
        ledc_update_duty(LEDC_MODE, LEDC_CH4);

        ledc_set_duty(LEDC_MODE, LEDC_CH5, LEDC_DUTY_foot_180);
        ledc_update_duty(LEDC_MODE, LEDC_CH5);

        ledc_set_duty(LEDC_MODE, LEDC_CH6, LEDC_DUTY_foot_180);
        ledc_update_duty(LEDC_MODE, LEDC_CH6);

        ledc_set_duty(LEDC_MODE, LEDC_CH7, LEDC_DUTY_foot_0);
        ledc_update_duty(LEDC_MODE, LEDC_CH7);

        break;

    case 2://四角站立
        ledc_set_duty(LEDC_MODE, LEDC_CH0, LEDC_DUTY_45);
        ledc_update_duty(LEDC_MODE, LEDC_CH0);

        ledc_set_duty(LEDC_MODE, LEDC_CH1, LEDC_DUTY_135);
        ledc_update_duty(LEDC_MODE, LEDC_CH1);

        ledc_set_duty(LEDC_MODE, LEDC_CH2, LEDC_DUTY_135);
        ledc_update_duty(LEDC_MODE, LEDC_CH2);

        ledc_set_duty(LEDC_MODE, LEDC_CH3, LEDC_DUTY_45);
        ledc_update_duty(LEDC_MODE, LEDC_CH3);

            vTaskDelay(2000 / portTICK_PERIOD_MS);

        ledc_set_duty(LEDC_MODE, LEDC_CH4, LEDC_DUTY_foot_45);
        ledc_update_duty(LEDC_MODE, LEDC_CH4);

        ledc_set_duty(LEDC_MODE, LEDC_CH5, LEDC_DUTY_foot_135);
        ledc_update_duty(LEDC_MODE, LEDC_CH5);

        ledc_set_duty(LEDC_MODE, LEDC_CH6, LEDC_DUTY_foot_135);
        ledc_update_duty(LEDC_MODE, LEDC_CH6);

        ledc_set_duty(LEDC_MODE, LEDC_CH7, LEDC_DUTY_foot_45);
        ledc_update_duty(LEDC_MODE, LEDC_CH7);

        

        break;

    case 3:
        ledc_set_duty(LEDC_MODE, LEDC_CH4, LEDC_DUTY_foot_0);
        ledc_update_duty(LEDC_MODE, LEDC_CH4);

        ledc_set_duty(LEDC_MODE, LEDC_CH5, LEDC_DUTY_foot_0);
        ledc_update_duty(LEDC_MODE, LEDC_CH5);

        vTaskDelay(1000 / portTICK_PERIOD_MS);

        ledc_set_duty(LEDC_MODE, LEDC_CH6, LEDC_DUTY_foot_0);
        ledc_update_duty(LEDC_MODE, LEDC_CH6);

        ledc_set_duty(LEDC_MODE, LEDC_CH7, LEDC_DUTY_foot_0);
        ledc_update_duty(LEDC_MODE, LEDC_CH7);
        break;
    
    case 4:
        ledc_set_duty(LEDC_MODE, LEDC_CH4, LEDC_DUTY_foot_180);
        ledc_update_duty(LEDC_MODE, LEDC_CH4);

        ledc_set_duty(LEDC_MODE, LEDC_CH5, LEDC_DUTY_foot_180);
        ledc_update_duty(LEDC_MODE, LEDC_CH5);

        ledc_set_duty(LEDC_MODE, LEDC_CH6, LEDC_DUTY_foot_180);
        ledc_update_duty(LEDC_MODE, LEDC_CH6);

        ledc_set_duty(LEDC_MODE, LEDC_CH7, LEDC_DUTY_foot_180);
        ledc_update_duty(LEDC_MODE, LEDC_CH7);
        break;

    case 5:
        ledc_set_duty(LEDC_MODE, LEDC_CH4, LEDC_DUTY_foot_90);
        ledc_update_duty(LEDC_MODE, LEDC_CH4);

        ledc_set_duty(LEDC_MODE, LEDC_CH5, LEDC_DUTY_foot_90);
        ledc_update_duty(LEDC_MODE, LEDC_CH5);

        ledc_set_duty(LEDC_MODE, LEDC_CH6, LEDC_DUTY_foot_90);
        ledc_update_duty(LEDC_MODE, LEDC_CH6);

        ledc_set_duty(LEDC_MODE, LEDC_CH7, LEDC_DUTY_foot_90);
        ledc_update_duty(LEDC_MODE, LEDC_CH7);
        break;

    default:
        break;
    }



}



void Timer0_init(void)
{
    // Prepare and then apply the LEDC PWM timer configuration
    ledc_timer_config_t ledc_timer0 = {
        .speed_mode       = LEDC_MODE,
        .timer_num        = LEDC_TIMER0,
        .duty_resolution  = LEDC_DUTY_RES,
        .freq_hz          = LEDC_FREQUENCY,  
        .clk_cfg          = LEDC_AUTO_CLK
    };
    ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer0));

    // Prepare and then apply the LEDC PWM channel configuration
    ledc_channel_config_t ledc_timer0_channel0 = {
        .speed_mode     = LEDC_MODE,
        .channel        = LEDC_CH0,
        .timer_sel      = LEDC_TIMER0,
        .intr_type      = LEDC_INTR_DISABLE,
        .gpio_num       = LEDC_TIMER0_CH0_IO,
        .duty           = 0, // Set duty to 0%
        .hpoint         = 0
    };
    ESP_ERROR_CHECK(ledc_channel_config(&ledc_timer0_channel0));

    ledc_channel_config_t ledc_timer0_channel1 = {
        .speed_mode     = LEDC_MODE,
        .channel        = LEDC_CH1,
        .timer_sel      = LEDC_TIMER0,
        .intr_type      = LEDC_INTR_DISABLE,
        .gpio_num       = LEDC_TIMER0_CH1_IO,
        .duty           = 0, // Set duty to 0%
        .hpoint         = 0
    };
    ESP_ERROR_CHECK(ledc_channel_config(&ledc_timer0_channel1));

    ledc_channel_config_t ledc_timer0_channel2 = {
        .speed_mode     = LEDC_MODE,
        .channel        = LEDC_CH2,
        .timer_sel      = LEDC_TIMER0,
        .intr_type      = LEDC_INTR_DISABLE,
        .gpio_num       = LEDC_TIMER0_CH2_IO,
        .duty           = 0, // Set duty to 0%
        .hpoint         = 0
    };
    ESP_ERROR_CHECK(ledc_channel_config(&ledc_timer0_channel2));

        ledc_channel_config_t ledc_timer0_channel3 = {
        .speed_mode     = LEDC_MODE,
        .channel        = LEDC_CH3,
        .timer_sel      = LEDC_TIMER0,
        .intr_type      = LEDC_INTR_DISABLE,
        .gpio_num       = LEDC_TIMER0_CH3_IO,
        .duty           = 0, // Set duty to 0%
        .hpoint         = 0
    };
    ESP_ERROR_CHECK(ledc_channel_config(&ledc_timer0_channel3));

    /*----------------------foot---------------------------------*/
            ledc_channel_config_t ledc_timer0_channel4 = {
        .speed_mode     = LEDC_MODE,
        .channel        = LEDC_CH4,
        .timer_sel      = LEDC_TIMER0,
        .intr_type      = LEDC_INTR_DISABLE,
        .gpio_num       = LEDC_TIMER0_CH4_IO,
        .duty           = 0, // Set duty to 0%
        .hpoint         = 0
    };
    ESP_ERROR_CHECK(ledc_channel_config(&ledc_timer0_channel4));

            ledc_channel_config_t ledc_timer0_channel5 = {
        .speed_mode     = LEDC_MODE,
        .channel        = LEDC_CH5,
        .timer_sel      = LEDC_TIMER0,
        .intr_type      = LEDC_INTR_DISABLE,
        .gpio_num       = LEDC_TIMER0_CH5_IO,
        .duty           = 0, // Set duty to 0%
        .hpoint         = 0
    };
    ESP_ERROR_CHECK(ledc_channel_config(&ledc_timer0_channel5));

            ledc_channel_config_t ledc_timer0_channel6 = {
        .speed_mode     = LEDC_MODE,
        .channel        = LEDC_CH6,
        .timer_sel      = LEDC_TIMER0,
        .intr_type      = LEDC_INTR_DISABLE,
        .gpio_num       = LEDC_TIMER0_CH6_IO,
        .duty           = 0, // Set duty to 0%
        .hpoint         = 0
    };
    ESP_ERROR_CHECK(ledc_channel_config(&ledc_timer0_channel6));

            ledc_channel_config_t ledc_timer0_channel7 = {
        .speed_mode     = LEDC_MODE,
        .channel        = LEDC_CH7,
        .timer_sel      = LEDC_TIMER0,
        .intr_type      = LEDC_INTR_DISABLE,
        .gpio_num       = LEDC_TIMER0_CH7_IO,
        .duty           = 0, // Set duty to 0%
        .hpoint         = 0
    };
    ESP_ERROR_CHECK(ledc_channel_config(&ledc_timer0_channel7));
}



























/*
void Timer1_init(void)
{
    // Prepare and then apply the LEDC PWM timer configuration
    ledc_timer_config_t ledc_timer = {
        .speed_mode       = LEDC_MODE,
        .timer_num        = LEDC_TIMER1,
        .duty_resolution  = LEDC_DUTY_RES,
        .freq_hz          = LEDC_FREQUENCY,  
        .clk_cfg          = LEDC_AUTO_CLK
    };
    ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));

    // Prepare and then apply the LEDC PWM channel configuration
    ledc_channel_config_t ledc_channel = {
        .speed_mode     = LEDC_MODE,
        .channel        = LEDC_CHANNEL,
        .timer_sel      = LEDC_TIMER1,
        .intr_type      = LEDC_INTR_DISABLE,
        .gpio_num       = LEDC_OUTPUT_IO,
        .duty           = 0, // Set duty to 0%
        .hpoint         = 0
    };
    ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel));
}

void Timer2_init(void)
{
    // Prepare and then apply the LEDC PWM timer configuration
    ledc_timer_config_t ledc_timer = {
        .speed_mode       = LEDC_MODE,
        .timer_num        = LEDC_TIMER2,
        .duty_resolution  = LEDC_DUTY_RES,
        .freq_hz          = LEDC_FREQUENCY,  
        .clk_cfg          = LEDC_AUTO_CLK
    };
    ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));

    // Prepare and then apply the LEDC PWM channel configuration
    ledc_channel_config_t ledc_channel = {
        .speed_mode     = LEDC_MODE,
        .channel        = LEDC_CHANNEL,
        .timer_sel      = LEDC_TIMER2,
        .intr_type      = LEDC_INTR_DISABLE,
        .gpio_num       = LEDC_OUTPUT_IO,
        .duty           = 0, // Set duty to 0%
        .hpoint         = 0
    };
    ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel));
}

void Timer3_init(void)
{
    // Prepare and then apply the LEDC PWM timer configuration
    ledc_timer_config_t ledc_timer = {
        .speed_mode       = LEDC_MODE,
        .timer_num        = LEDC_TIMER3,
        .duty_resolution  = LEDC_DUTY_RES,
        .freq_hz          = LEDC_FREQUENCY,  
        .clk_cfg          = LEDC_AUTO_CLK
    };
    ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));

    // Prepare and then apply the LEDC PWM channel configuration
    ledc_channel_config_t ledc_channel = {
        .speed_mode     = LEDC_MODE,
        .channel        = LEDC_CHANNEL,
        .timer_sel      = LEDC_TIMER3,
        .intr_type      = LEDC_INTR_DISABLE,
        .gpio_num       = LEDC_OUTPUT_IO,
        .duty           = 0, // Set duty to 0%
        .hpoint         = 0
    };
    ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel));
}


*/